using Microsoft.VisualBasic;
using Microsoft.VisualBasic.CompilerServices;
using SJVars;
using System;
using System.Collections;
using yW4Lib;

namespace yW5Lib
{
	public class clsImportYW4
	{
		private clsProjectIO cProjectIO;

		public clsImportYW4()
		{
			cProjectIO = new clsProjectIO();
		}

		public bool ImportYW4(ref clsProject cProject, string YW4File = "")
		{
			//Discarded unreachable code: IL_051e, IL_0535, IL_0540, IL_0550, IL_06a2, IL_06a4, IL_06b8, IL_06da
			int num = default(int);
			bool result = false;
			int num4 = default(int);
			try
			{
				ProjectData.ClearProjectError();
				num = 2;
				int num2 = 2;
				result = true;
				num2 = 3;
				clsYW4Project clsYW4Project = new clsYW4Project();
				num2 = 4;
				if (Operators.CompareString(YW4File, "", TextCompare: false) == 0)
				{
					num2 = 5;
					YW4File = clsShared.cFileSel.GetFile(clsShared.cSpecialFolders.MyDocsFolder, "*.YW4");
				}
				num2 = 6;
				if (Operators.CompareString(YW4File, "", TextCompare: false) == 0)
				{
					num2 = 7;
					result = false;
				}
				else
				{
					num2 = 9;
					if (Strings.InStr(YW4File, ".zip\\", CompareMethod.Text) > 0)
					{
						num2 = 10;
						Interaction.MsgBox("You can't load a yWriter project from a zip archive. Please extract the zip to a folder first.");
						num2 = 11;
						result = false;
					}
					else
					{
						num2 = 13;
						if (!clsYW4Project.ImportYW4(YW4File))
						{
							num2 = 14;
							Interaction.MsgBox("There was an error loading your YW4 project");
							num2 = 15;
							result = false;
						}
						else
						{
							num2 = 18;
							if (cProject.CreateEmptyProject(V6Project: false))
							{
								num2 = 19;
								cProject.Title = clsYW4Project.Title;
								num2 = 20;
								cProject.AuthorName = clsYW4Project.AuthorName;
								num2 = 21;
								int num3 = 1;
								do
								{
									num2 = 22;
									cProject.set_DeadlineDate(num3, clsYW4Project.get_DeadlineDate(num3));
									num2 = 23;
									cProject.set_DeadlineStartDate(num3, clsYW4Project.get_DeadlineStartDate(num3));
									num2 = 24;
									num3++;
								}
								while (num3 <= 5);
								num2 = 25;
								cProject.WordCountStart = clsYW4Project.WordCountStart;
								num2 = 26;
								cProject.WordTarget = clsYW4Project.WordTarget;
								num2 = 27;
								cProject.WordTargetEndDate = clsYW4Project.WordTargetEndDate;
								num2 = 28;
								cProject.WordTargetStartDate = clsYW4Project.WordTargetStartDate;
								num2 = 29;
								cProject.AuthorBio = clsYW4Project.AuthorBio;
								num2 = 30;
								cProject.DescText = clsYW4Project.DescText;
								num2 = 31;
								cProject.EndBeginningChID = clsYW4Project.EndBeginningChID;
								num2 = 32;
								cProject.EndMiddleChID = clsYW4Project.EndMiddleChID;
								num2 = 33;
								num3 = 1;
								do
								{
									num2 = 34;
									cProject.set_FieldTitle(num3, clsYW4Project.get_FieldTitle(num3));
									num2 = 35;
									num3++;
								}
								while (num3 <= 4);
								num2 = 36;
								IEnumerator enumerator = clsYW4Project.Notes.GetEnumerator();
								while (enumerator.MoveNext())
								{
									clsYW4Note YW4Note = (clsYW4Note)enumerator.Current;
									num2 = 37;
									clsNote cNote = new clsNote();
									num2 = 38;
									TransferNote(ref YW4Note, ref cNote);
									num2 = 39;
									cProject.AddNote(ref cNote);
									num2 = 40;
								}
								num2 = 41;
								if (enumerator is IDisposable)
								{
									(enumerator as IDisposable).Dispose();
								}
								num2 = 42;
								IEnumerator enumerator2 = clsYW4Project.Chars.GetEnumerator();
								while (enumerator2.MoveNext())
								{
									clsYW4Character YW4Char = (clsYW4Character)enumerator2.Current;
									num2 = 43;
									clsCharacter cCh = new clsCharacter();
									num2 = 44;
									TransferChar(ref YW4Char, ref cCh);
									num2 = 45;
									cProject.AddChar(ref cCh);
									num2 = 46;
									if (cCh.ID != YW4Char.ID)
									{
										num2 = 47;
										clsShared.cLog.AddAction("Char ID " + Conversions.ToString(YW4Char.ID) + " changed to " + Conversions.ToString(cCh.ID) + " in new project.");
									}
									num2 = 48;
								}
								num2 = 49;
								if (enumerator2 is IDisposable)
								{
									(enumerator2 as IDisposable).Dispose();
								}
								num2 = 50;
								IEnumerator enumerator3 = clsYW4Project.Items.GetEnumerator();
								while (enumerator3.MoveNext())
								{
									clsYW4Item YW4Item = (clsYW4Item)enumerator3.Current;
									num2 = 51;
									clsItem cItem = new clsItem();
									num2 = 52;
									TransferItem(ref YW4Item, ref cItem);
									num2 = 53;
									cProject.AddItem(ref cItem);
									num2 = 54;
								}
								num2 = 55;
								if (enumerator3 is IDisposable)
								{
									(enumerator3 as IDisposable).Dispose();
								}
								num2 = 56;
								IEnumerator enumerator4 = clsYW4Project.Locations.GetEnumerator();
								while (enumerator4.MoveNext())
								{
									clsYW4Location YW4Location = (clsYW4Location)enumerator4.Current;
									num2 = 57;
									clsLocation cLocation = new clsLocation();
									num2 = 58;
									TransferLocation(ref YW4Location, ref cLocation);
									num2 = 59;
									cProject.AddLocation(ref cLocation);
									num2 = 60;
								}
								num2 = 61;
								if (enumerator4 is IDisposable)
								{
									(enumerator4 as IDisposable).Dispose();
								}
								num2 = 62;
								IEnumerator enumerator5 = clsYW4Project.Chapters.GetEnumerator();
								while (enumerator5.MoveNext())
								{
									clsYW4Chapter YW4Chapter = (clsYW4Chapter)enumerator5.Current;
									num2 = 63;
									clsChapter cChapter = new clsChapter();
									num2 = 64;
									TransferChapter(ref YW4Chapter, ref cChapter);
									num2 = 65;
									cProject.AddChapter(ref cChapter);
									num2 = 66;
								}
								num2 = 67;
								if (enumerator5 is IDisposable)
								{
									(enumerator5 as IDisposable).Dispose();
								}
								num2 = 68;
								IEnumerator enumerator6 = clsYW4Project.Scenes.GetEnumerator();
								while (enumerator6.MoveNext())
								{
									clsYW4Scene YW4Scene = (clsYW4Scene)enumerator6.Current;
									num2 = 69;
									clsScene Scene = new clsScene();
									num2 = 70;
									Scene.V6 = cProject.V6File;
									num2 = 71;
									TransferScene(ref YW4Scene, ref Scene);
									num2 = 72;
									clsShared.cLog.AddAction("Setting BelongsToChID to " + Conversions.ToString(Scene.BelongsToChID) + " in scene ID: " + Conversions.ToString(Scene.ID));
									num2 = 73;
									cProject.AddSceneToProjectAndChapter(ref Scene, Scene.BelongsToChID);
									num2 = 74;
								}
								num2 = 75;
								if (enumerator6 is IDisposable)
								{
									(enumerator6 as IDisposable).Dispose();
								}
							}
							num2 = 76;
							if (cProject.CriticalSave())
							{
								num2 = 77;
								result = true;
							}
						}
					}
				}
			}
			catch (Exception obj) when ((obj is Exception && num != 0) & (num4 == 0))
			{
				ProjectData.SetProjectError((Exception)obj);
				/*Error near IL_06d8: Could not find block for branch target IL_06a4*/;
			}
			if (num4 != 0)
			{
				ProjectData.ClearProjectError();
			}
			return result;
		}

		private void TransferChar(ref clsYW4Character YW4Char, ref clsCharacter cCh)
		{
			//Discarded unreachable code: IL_00df, IL_00f6, IL_0101, IL_0111, IL_0167, IL_0169, IL_017d, IL_019f
			int num = default(int);
			int num3 = default(int);
			try
			{
				ProjectData.ClearProjectError();
				num = 2;
				int num2 = 2;
				cCh.AKA = YW4Char.AlternateNames;
				num2 = 3;
				cCh.Bio = YW4Char.Bio;
				num2 = 4;
				cCh.DescText = YW4Char.DescText;
				num2 = 5;
				cCh.FullName = YW4Char.FullName;
				num2 = 6;
				cCh.Goals = YW4Char.Goals;
				num2 = 7;
				cCh.ID = YW4Char.ID;
				num2 = 8;
				cCh.ImageFile = YW4Char.ImageFile;
				num2 = 9;
				cCh.Major = YW4Char.Major;
				num2 = 10;
				cCh.Notes = YW4Char.Notes;
				num2 = 11;
				cCh.SortOrder = YW4Char.SortOrder;
				num2 = 12;
				cCh.Tags = YW4Char.Tags;
				num2 = 13;
				cCh.Title = YW4Char.Title;
				num2 = 14;
				cCh.Unused = YW4Char.Unused;
			}
			catch (Exception obj) when ((obj is Exception && num != 0) & (num3 == 0))
			{
				ProjectData.SetProjectError((Exception)obj);
				/*Error near IL_019d: Could not find block for branch target IL_0169*/;
			}
			if (num3 != 0)
			{
				ProjectData.ClearProjectError();
			}
		}

		private void TransferItem(ref clsYW4Item YW4Item, ref clsItem cItem)
		{
			//Discarded unreachable code: IL_0079, IL_0090, IL_009b, IL_00ab, IL_00e9, IL_00eb, IL_00ff, IL_0121
			int num = default(int);
			int num3 = default(int);
			try
			{
				ProjectData.ClearProjectError();
				num = 2;
				int num2 = 2;
				cItem.DescText = YW4Item.DescText;
				num2 = 3;
				cItem.ID = YW4Item.ID;
				num2 = 4;
				cItem.ImageFile = YW4Item.ImageFile;
				num2 = 5;
				cItem.SortOrder = YW4Item.SortOrder;
				num2 = 6;
				cItem.Tags = YW4Item.Tags;
				num2 = 7;
				cItem.Title = YW4Item.Title;
				num2 = 8;
				cItem.Unused = YW4Item.Unused;
			}
			catch (Exception obj) when ((obj is Exception && num != 0) & (num3 == 0))
			{
				ProjectData.SetProjectError((Exception)obj);
				/*Error near IL_011f: Could not find block for branch target IL_00eb*/;
			}
			if (num3 != 0)
			{
				ProjectData.ClearProjectError();
			}
		}

		private void TransferLocation(ref clsYW4Location YW4Location, ref clsLocation cLocation)
		{
			//Discarded unreachable code: IL_0079, IL_0090, IL_009b, IL_00ab, IL_00e9, IL_00eb, IL_00ff, IL_0121
			int num = default(int);
			int num3 = default(int);
			try
			{
				ProjectData.ClearProjectError();
				num = 2;
				int num2 = 2;
				cLocation.DescText = YW4Location.DescText;
				num2 = 3;
				cLocation.ID = YW4Location.ID;
				num2 = 4;
				cLocation.ImageFile = YW4Location.ImageFile;
				num2 = 5;
				cLocation.SortOrder = YW4Location.SortOrder;
				num2 = 6;
				cLocation.Tags = YW4Location.Tags;
				num2 = 7;
				cLocation.Title = YW4Location.Title;
				num2 = 8;
				cLocation.Unused = YW4Location.Unused;
			}
			catch (Exception obj) when ((obj is Exception && num != 0) & (num3 == 0))
			{
				ProjectData.SetProjectError((Exception)obj);
				/*Error near IL_011f: Could not find block for branch target IL_00eb*/;
			}
			if (num3 != 0)
			{
				ProjectData.ClearProjectError();
			}
		}

		private void TransferScene(ref clsYW4Scene YW4Scene, ref clsScene Scene)
		{
			//Discarded unreachable code: IL_0234, IL_024b, IL_0256, IL_0266, IL_0310, IL_0312, IL_0326, IL_0348
			int num = default(int);
			int num5 = default(int);
			try
			{
				ProjectData.ClearProjectError();
				num = 2;
				int num2 = 2;
				Scene.BelongsToChID = YW4Scene.BelongsToChID;
				num2 = 3;
				Scene.Conflict = YW4Scene.Conflict;
				num2 = 4;
				Scene.Day_Renamed = YW4Scene.Day_Renamed;
				num2 = 5;
				Scene.DescText = YW4Scene.DescText;
				num2 = 6;
				int num3 = 1;
				do
				{
					num2 = 7;
					((clsSceneBase)Scene).set_Field(num3, YW4Scene.get_Field(num3));
					num2 = 8;
					num3++;
				}
				while (num3 <= 4);
				num2 = 9;
				Scene.Goal = YW4Scene.Goal;
				num2 = 10;
				Scene.Hour_Renamed = YW4Scene.Hour_Renamed;
				num2 = 11;
				Scene.ID = YW4Scene.ID;
				num2 = 12;
				Scene.ImageFile = YW4Scene.ImageFile;
				num2 = 13;
				Scene.LastsDays = YW4Scene.LastsDays;
				num2 = 14;
				Scene.LastsHours = YW4Scene.LastsHours;
				num2 = 15;
				Scene.LastsMinutes = YW4Scene.LastsMinutes;
				num2 = 16;
				Scene.Minute_Renamed = YW4Scene.Minute_Renamed;
				num2 = 17;
				Scene.Notes = YW4Scene.Notes;
				num2 = 18;
				Scene.Outcome = YW4Scene.Outcome;
				num2 = 19;
				Scene.ReactionScene = YW4Scene.ReactionScene;
				num2 = 20;
				Scene.Status = YW4Scene.Status;
				num2 = 21;
				Scene.SubPlot = YW4Scene.SubPlot;
				num2 = 22;
				Scene.Tags = YW4Scene.Tags;
				num2 = 23;
				Scene.TextRTF = YW4Scene.TextRTF;
				num2 = 24;
				Scene.Title = YW4Scene.Title;
				num2 = 25;
				Scene.Unused = YW4Scene.Unused;
				num2 = 26;
				int itemCount = YW4Scene.ItemCount;
				for (int i = 1; i <= itemCount; i++)
				{
					num2 = 27;
					Scene.AddItemByID(YW4Scene.ItemIDByIndex(i));
					num2 = 28;
				}
				num2 = 29;
				int charCount = YW4Scene.CharCount;
				for (int i = 1; i <= charCount; i++)
				{
					num2 = 30;
					Scene.AddCharByID(YW4Scene.CharIDByIndex(i));
					num2 = 31;
				}
				num2 = 32;
				int num4 = YW4Scene.LocationCount();
				for (int i = 1; i <= num4; i++)
				{
					num2 = 33;
					Scene.AddLocationByID(YW4Scene.LocationIDByIndex(i));
					num2 = 34;
				}
				num2 = 35;
				Scene.SetViewpointByID(YW4Scene.ViewPointID);
			}
			catch (Exception obj) when ((obj is Exception && num != 0) & (num5 == 0))
			{
				ProjectData.SetProjectError((Exception)obj);
				/*Error near IL_0346: Could not find block for branch target IL_0312*/;
			}
			if (num5 != 0)
			{
				ProjectData.ClearProjectError();
			}
		}

		private void TransferChapter(ref clsYW4Chapter YW4Chapter, ref clsChapter cChapter)
		{
			cChapter.DescText = YW4Chapter.DescText;
			cChapter.ID = YW4Chapter.ID;
			cChapter.SortOrder = YW4Chapter.SortOrder;
			cChapter.Tags = YW4Chapter.Tags;
			cChapter.Title = YW4Chapter.Title;
			cChapter.Type = ChapterTypes.ChapterNormal;
			cChapter.Unused = YW4Chapter.Unused;
			int sceneCount = YW4Chapter.SceneCount;
			for (int i = 1; i <= sceneCount; i++)
			{
				int num = YW4Chapter.SceneIDByIndex(i);
				cChapter.AddSceneID(num);
				clsShared.cLog.AddAction("Added scene ID " + Conversions.ToString(num) + " to chapter ID " + Conversions.ToString(YW4Chapter.ID));
			}
			int sceneCount2 = cChapter.SceneCount;
			for (int i = 1; i <= sceneCount2; i++)
			{
				clsShared.cLog.AddAction("In new chapter, scene #" + Conversions.ToString(i) + "'s ID is " + Conversions.ToString(cChapter.get_SceneIDByIndex(i)));
			}
		}

		private void TransferNote(ref clsYW4Note YW4Note, ref clsNote cNote)
		{
			cNote.DescText = YW4Note.DescText;
			cNote.ID = YW4Note.ID;
			cNote.SortOrder = YW4Note.SortOrder;
			cNote.Tags = YW4Note.Tags;
			cNote.Title = YW4Note.Title;
		}
	}
}
